﻿using Authority.DataTransferObject.Routing;
using Authority.IServices;
using Authority.IServices.Model.Routing;
using Authority.PresentationModels.Routing;
using AutoMapper;
using HelperFromTheSkeleton.APICore;
using HelperFromTheSkeleton.Model.RequestModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Authority.WebAPI.Controllers
{
    /// <summary>
    /// 路由权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoutingAuthorityController : BaseController
    {

        private readonly IRoutingAuthorityService  _routingAuthorityService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="routingAuthorityService"></param>
        /// <param name="mapper"></param>
        public RoutingAuthorityController(IRoutingAuthorityService  routingAuthorityService, IMapper mapper)
        {
            _routingAuthorityService = routingAuthorityService;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增路由
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Add(AddRoutingRequestModel requestModel)
        {
            return await HandleAsync(async() =>
            {
                var model = _mapper.Map<AddRoutingModel>(requestModel);
                await _routingAuthorityService.AddAsync(model);
            });
        }

        /// <summary>
        /// 修改路由
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel> Edit(EditRoutingRequestModel requestModel)
        {
            return await HandleAsync(async () =>
            {
                var model = _mapper.Map<EditRoutingModel>(requestModel);
                await _routingAuthorityService.EditAsync(model);
            });
        }

        /// <summary>
        /// 删除路由
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel> Delete([Required]Guid id)
        {
            return await HandleAsync(async () =>
            {
                await _routingAuthorityService.DeleteAsync(id);
            });
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="ids">唯一标识集</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<RoutingAuthorityDTO>>> Get([Required] List<Guid> ids)
        {
            return await HandleAsync(async () =>
            {
                List<RoutingAuthorityDTO> result = await _routingAuthorityService.GetAsync(ids);
                return result;
            });
        }

        /// <summary>
        /// 获取一个层级
        /// </summary>
        /// <param name="parnetID">父级别ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<RoutingAuthorityListDTO>>> GetOneLevel(Guid? parnetID)
        {
            return await HandleAsync(async () =>
            {
                List<RoutingAuthorityListDTO> result = await _routingAuthorityService.GetOneLevelAsync(parnetID);
                return result;
            });
        }

        /// <summary>
        /// 获取路由树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<RoutingTreeDTO>>> GetTree(Guid SubsystemID)
        {
            return await HandleAsync(async () =>
            {
                List<RoutingTreeDTO> result = await _routingAuthorityService.GetTreeAsync(SubsystemID);
                return result;
            });
        }

    }
}
